import 'package:flutter/material.dart';
import 'package:date_format/date_format.dart';
//国际化配置
import 'package:flutter_localizations/flutter_localizations.dart';

void main() {
  runApp(MyApp());
}

//自定义组件
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // MaterialApp是根组件
    return MaterialApp(
      home: Scaffold(
          appBar: AppBar(
            title: Text('Date Time'),
          ),
          body: DatePicker()),
      theme: ThemeData(primaryColor: Colors.blue),
      //国际化配置
      localizationsDelegates: [
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate
      ],
      //国际化配置
      supportedLocales: [const Locale('zh', 'CH'), const Locale('en', 'US')],
      //国际化配置
      locale: Locale('zh'),
    );
  }
}

class DatePicker extends StatefulWidget {
  DatePicker({Key? key}) : super(key: key);

  @override
  _DatePickerState createState() => _DatePickerState();
}

class _DatePickerState extends State<DatePicker> {
  var now = DateTime.now();
  DateTime? _nowDate = DateTime.now();
  TimeOfDay? _nowTime = TimeOfDay.now();
  @override
  void initState() {
    super.initState();
    //打印当前日期
    print(now);
    print("时间戳：" + now.microsecondsSinceEpoch.toString());
    //
    print("时间戳转回时间:" +
        DateTime.fromMillisecondsSinceEpoch(now.microsecondsSinceEpoch)
            .toString());
    //date_format库
    print("date_format库：" +
        formatDate(DateTime(1989, 2, 21), [yyyy, '-', mm, '-', dd]));
  }

  // _showDatePicker() {
  //   showDatePicker(
  //           context: context,
  //           initialDate: DateTime.now(),
  //           firstDate: DateTime(1989, 2, 21),
  //           lastDate: DateTime(9999, 2, 21))
  //       .then((value) {
  //     print(value);
  //   });
  // }
  //另一种写法
  // _showDatePicker() async {
  //   _nowDate = (await showDatePicker(
  //       context: context,
  //       initialDate: DateTime.now(),
  //       firstDate: DateTime(1989, 2, 21),
  //       lastDate: DateTime(9999, 2, 21)));
  // }

  _showDatePicker() async {
    var result = (await showDatePicker(
        context: context,
        initialDate: DateTime.now(),
        firstDate: DateTime(1984, 2, 21),
        lastDate: DateTime(9999, 2, 21),
        //国际化配置
        locale: Locale('zh')));

    setState(() {
      this._nowDate = result == null ? DateTime.now() : result;
    });
  }

  _showTimePicker() async {
    TimeOfDay? res =
        await showTimePicker(context: context, initialTime: TimeOfDay.now());

    setState(() {
      this._nowTime = res == null ? TimeOfDay.now() : res;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("DatePicker"),
        ),
        body: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            //Row没有点击时间用InkWell的替代
            InkWell(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Text(formatDate(_nowDate!, [yyyy, '-', mm, '-', dd])),
                  Icon(Icons.arrow_drop_down)
                ],
              ),
              onTap: _showDatePicker,
            ),
            InkWell(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Text("${_nowTime!.format(context)}"),
                  Icon(Icons.arrow_drop_down)
                ],
              ),
              onTap: _showTimePicker,
            )
          ],
        ));
  }
}
